package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* BackgroundPlayView
* */
class BackgroundPlayView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mStrokeWidth = context.resources.getDimension(R.dimen.dp_3)

    private val mPaint by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeWidth = mStrokeWidth
            strokeCap = Paint.Cap.ROUND
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_17A4DC)
        }
    }

    private val mPath by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            translate(cx, cy + (cy / 3 - cy  * 2 / 5) / 2)
            mPath.apply {
                reset()
                addRoundRect(
                    -cx * 4 / 5,
                    cy * 1 / 6,
                    -cx * 2 / 5,
                    cy * 2 / 3,
                    cx / 6,
                    cx / 6,
                    Path.Direction.CW
                )

                moveTo(-cx * 3 / 5, cy * 1 / 6)
                lineTo(-cx * 3 / 5, 0F)

                val arcX = cx * 3 / 5
                val arcY = cy * 3 / 5
                arcTo(
                    -arcX,
                    -arcY,
                    arcX,
                    arcY,
                    180F,
                    180F,
                    false
                )

                lineTo(cx * 3 / 5, cy * 1 / 6)

                addRoundRect(
                    cx * 4 / 5,
                    cy * 1 / 6,
                    cx * 2 / 5,
                    cy * 2 / 3,
                    cx / 6,
                    cx / 6,
                    Path.Direction.CW
                )
            }
            drawPath(mPath, mPaint)
        }
    }
}